package com.hzw.saas.web.admin.rbac.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hzw.saas.api.rbac.IRoleService;
import com.hzw.saas.api.rbac.bo.RoleBO;
import com.hzw.saas.api.rbac.bo.RoleDetailBO;
import com.hzw.saas.api.rbac.query.RoleAddQuery;
import com.hzw.saas.api.rbac.query.RoleUpdateQuery;
import com.hzw.saas.common.config.annotation.SysLog;
import com.hzw.saas.common.config.util.PageParam;
import com.hzw.saas.web.admin.rbac.param.RoleIdsParam;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zzl
 * @since 08/31/2021
 */
@RestController
@Slf4j
@RequiredArgsConstructor
@Api(tags = "授权管理/角色管理")
public class RoleController {

    private final IRoleService roleService;

    @GetMapping("/sys/roles/page")
    @PreAuthorize("@pms.assertAny(@baseAuth.SYS_ROLE_LIST)")
    @SysLog
    @ApiOperation(value = "获取角色列表", notes = "分页获取角色列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "sys", value = "所属系统, 1:后台管理用户, 2:云平台用户", dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "fuzzyRoleName", value = "角色名(支持模糊)", dataType = "String", paramType = "query")
    })
    @ApiOperationSort(11)
    public ResponseEntity<IPage<RoleBO>> pageRoles(PageParam pageParam, Integer sys, String fuzzyRoleName) {
        IPage<RoleBO> rolesPage = roleService.pageList(pageParam, sys, fuzzyRoleName);
        return ResponseEntity.ok(rolesPage);
    }

    @GetMapping("/sys/role/{roleId}/detail")
    @PreAuthorize("@pms.assertAny(@baseAuth.SYS_ROLE_GET)")
    @SysLog
    @ApiOperation(value = "查看角色详情")
    @ApiImplicitParams(
        @ApiImplicitParam(name = "roleId", value = "角色ID", dataType = "String", paramType = "path", required = true)
    )
    @ApiOperationSort(12)
    public ResponseEntity<RoleDetailBO> getRoleDetail(@PathVariable String roleId) {
        RoleDetailBO roleDetailBO = roleService.get(roleId);
        return ResponseEntity.ok(roleDetailBO);
    }

    @PostMapping("/sys/role")
    @PreAuthorize("@pms.assertAny(@baseAuth.SYS_ROLE_SAVE)")
    @SysLog(operation = "增加角色", mode = 3)
    @ApiOperation(value = "增加角色", notes = "增加角色，同时设定角色权限")
    @ApiOperationSort(13)
    public ResponseEntity<Void> addRole(@Validated @RequestBody RoleAddQuery param) {
        roleService.add(param);
        return ResponseEntity.ok().build();
    }

    @PutMapping("/sys/role")
    @SysLog(operation = "更新角色", mode = 3)
    @PreAuthorize("@pms.assertAny(@baseAuth.SYS_ROLE_UPDATE)")
    @ApiOperation(value = "更新角色", notes = "更新角色，同时设定角色权限")
    @ApiOperationSort(14)
    public ResponseEntity<Void> updateRole(@Validated @RequestBody RoleUpdateQuery param) {
        roleService.update(param);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/sys/role/{roleId}")
    @PreAuthorize("@pms.assertAny(@baseAuth.SYS_ROLE_DELETE)")
    @SysLog(operation = "删除角色", mode = 3)
    @ApiOperation(value = "删除角色", notes = "删除角色")
    @ApiImplicitParams(
        @ApiImplicitParam(name = "roleId", value = "角色ID", dataType = "String", paramType = "path", required = true)
    )
    @ApiOperationSort(15)
    public ResponseEntity<Void> deleteRole(@PathVariable String roleId) {
        roleService.delete(roleId);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/sys/roles")
    @PreAuthorize("@pms.assertAny(@baseAuth.SYS_ROLE_DELETE)")
    @SysLog(operation = "批量删除角色", mode = 3)
    @ApiOperation(value = "批量删除角色", notes = "批量删除角色")
    @ApiOperationSort(16)
    public ResponseEntity<Void> deleteRoles(@Validated @RequestBody RoleIdsParam param) {
        roleService.deleteBatch(param.getRoleIds());
        return ResponseEntity.ok().build();
    }


}
